# -- coding: utf-8 --
# @File : OperateDBInterface.py
# @Time : 2024/8/19
# @Author : RLH
# 功能描述: 操作数据库的统一接口, 抽象产品类

from abc import ABC, abstractmethod
import uuid
from datetime import datetime, timedelta, date


class OperateDBInterface(ABC):

    def get_uuid(self):
        return str(uuid.uuid1())

    def get_datetime(self, format="%Y-%m-%d %H:%M:%S", **kwargs ):
        # days=0, seconds=0, microseconds=0,  milliseconds=0, minutes=0, hours=0, weeks=0
        now = datetime.now()
        three_minutes = timedelta(**kwargs)
        date_time = now - three_minutes
        date_time = date_time.strftime(format)
        return date_time

    # @abstractmethod
    def select_SQL(self, SQL_template, params=None):
        """执行查询并返回结果"""
        # with self.connection.cursor() as cursor:
        cursor = self.connection.cursor()
        if params:
            cursor.execute(SQL_template, params)
        else:
            cursor.execute(SQL_template)
        return cursor.fetchall()

    # @abstractmethod
    def insert_SQL(self, SQL_template, params=None):
        cursor = self.connection.cursor()
        # print("Executing SQL:", SQL_template)
        if params:
            params_str = ', '.join([str(param) for param in params])
            # print("With parameters:", params_str)
            cursor.execute(SQL_template, params)
        else:
            # print("No parameters provided.")
            cursor.execute(SQL_template)
        self.connection.commit()

        # print("SQL插入成功")

    # @abstractmethod
    def delete_SQL(self, SQL_template, params=None):
        cursor = self.connection.cursor()
        if params:
            params_str = ', '.join([str(param) for param in params])
            cursor.execute(SQL_template, params)
        else:
            cursor.execute(SQL_template)
        self.connection.commit()


    def execute_SQL(self, SQL):
        cursor = self.connection.cursor()
        cursor.execute(SQL)
        self.connection.commit()




    # @abstractmethod
    def close(self):
        """关闭数据库连接"""
        if self.connection:
            self.connection.close()
            self.connection = None


